/**
 * @title TouzhuMatchListAdapter.java
 * @package com.esun.cheetah.view.personal
 * @description 已投注比赛列表数据适配器
 * @author liugan
 * @date 2012-5-8
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.personal.adapter;

import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.attention.MatchState;
import com.esun.cheetah.model.personal.attention.AttentionGroupInfo.GroupState;
import com.esun.cheetah.model.personal.attention.MatchInfo;

/**
 * @title TouzhuMatchListAdapter
 * @description 已投注比赛列表数据适配器
 * @author liugan
 */
public class TouzhuMatchListAdapter extends AbsAttentionAdapter {
	private ArrayList<ArrayList<MatchInfo>> mChildList;

	public TouzhuMatchListAdapter(Context context) {
		super(context);
		mChildList = new ArrayList<ArrayList<MatchInfo>>();
		mChildList.add(new ArrayList<MatchInfo>());
		mChildList.add(new ArrayList<MatchInfo>());
		mChildList.add(new ArrayList<MatchInfo>());
	}

	public void setChildList(ArrayList<MatchInfo> rightNowList, int position) {
		mChildList.get(position).clear();
		mChildList.get(position).addAll(rightNowList);
		setGroupState(rightNowList.size(), position);
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder childViewHolder;
		if (convertView == null) {
			convertView = (View) LayoutInflater.from(context).inflate(
					R.layout.touzhu_match_info_item, null);
			childViewHolder = new ChildViewHolder();
			childViewHolder.rlMatchContainer = (RelativeLayout) convertView
					.findViewById(R.id.rl_touzhu_match_list_info_container);
			childViewHolder.tvLeagueName = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_league_name);
			childViewHolder.tvHostNumber = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_host_team_number);
			childViewHolder.tvHostName = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_host_team_name);
			childViewHolder.tvHostScore = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_host_team_score);
			childViewHolder.tvGuestNumber = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_guest_team_number);
			childViewHolder.tvGuestName = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_guest_team_name);
			childViewHolder.tvGuestScore = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_guest_team_score);
			childViewHolder.tvTimeState = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_match_time_state);
			childViewHolder.rlEmptyContainer = (RelativeLayout) convertView
					.findViewById(R.id.rl_touzhu_match_list_no_record_container);
			childViewHolder.rlPeilvContainer = (RelativeLayout) convertView
					.findViewById(R.id.rl_touzhu_match_list_item_match_peilv_container);
			childViewHolder.tvResult = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_match_result);
			childViewHolder.tvResultValue = (TextView) convertView
					.findViewById(R.id.tv_touzhu_match_list_item_match_result_peilv);
			childViewHolder.rlProgress = (RelativeLayout) convertView
					.findViewById(R.id.rl_touzhu_match_list_item_loading_container);
			convertView.setTag(childViewHolder);
		} else {
			childViewHolder = (ChildViewHolder) convertView.getTag();
		}

		final GroupState state = mGroupList.get(groupPosition).getState();

		if (state == GroupState.normal) {
			// 设置比赛项比分相关内容
			MatchInfo matchInfo = mChildList.get(groupPosition).get(
					childPosition);
			childViewHolder.tvLeagueName.setText(matchInfo.getLeagueName());
			childViewHolder.tvHostNumber.setText(matchInfo.getHostNumber());
			childViewHolder.tvHostName.setText(matchInfo.getHostName());
			childViewHolder.tvGuestNumber.setText(matchInfo.getGuestNumber());
			childViewHolder.tvGuestName.setText(matchInfo.getGuestName());

			Resources resources = context.getResources();
			switch (matchInfo.getClassifyState()) {

			case rightNow:
				childViewHolder.tvTimeState.setTextColor(resources
						.getColor(R.color.match_state_right_now));
				childViewHolder.tvTimeState.setText(matchInfo
						.getStateDescription());
				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_ing);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_ing);
				childViewHolder.tvHostScore.setText(String.valueOf(matchInfo
						.getHostScore()));
				childViewHolder.tvGuestScore.setText(String.valueOf(matchInfo
						.getGuestScore()));

				break;
			case future:
				childViewHolder.tvTimeState.setTextColor(resources
						.getColor(R.color.match_state_future));
				childViewHolder.tvTimeState.setText(matchInfo
						.getStateDescription());

				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_soon);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_soon);
				childViewHolder.tvHostScore.setText("");
				childViewHolder.tvGuestScore.setText("");
				break;
			case done:
				childViewHolder.tvHostScore
						.setBackgroundResource(R.drawable.img_default_score_end);
				childViewHolder.tvGuestScore
						.setBackgroundResource(R.drawable.img_default_score_end);
				childViewHolder.tvHostScore.setText(String.valueOf(matchInfo
						.getHostScore()));
				childViewHolder.tvGuestScore.setText(String.valueOf(matchInfo
						.getGuestScore()));
				if (matchInfo.getState() == MatchState.end) {
					childViewHolder.rlPeilvContainer
							.setVisibility(View.VISIBLE);
					childViewHolder.tvTimeState.setVisibility(View.INVISIBLE);
					if (matchInfo.getHostScore() > matchInfo.getGuestScore()) {
						childViewHolder.tvResult.setTextColor(resources
								.getColor(R.color.win));
						childViewHolder.tvResult.setText("胜");
						childViewHolder.tvResultValue.setTextColor(resources
								.getColor(R.color.win));
						childViewHolder.tvResultValue.setText(matchInfo
								.getWin());
					} else if (matchInfo.getHostScore() < matchInfo
							.getGuestScore()) {
						childViewHolder.tvResult.setTextColor(resources
								.getColor(R.color.lose));
						childViewHolder.tvResult.setText("负");
						childViewHolder.tvResultValue.setTextColor(resources
								.getColor(R.color.lose));
						childViewHolder.tvResultValue.setText(matchInfo
								.getLose());
					} else {
						childViewHolder.tvResult.setTextColor(Color.BLACK);
						childViewHolder.tvResult.setText("平");
						childViewHolder.tvResultValue.setTextColor(Color.BLACK);
						childViewHolder.tvResultValue.setText(matchInfo
								.getDraw());
					}
				} else {
					childViewHolder.tvTimeState.setTextColor(Color.GRAY);

				}

				break;
			default:
				break;
			}

			childViewHolder.tvTimeState
					.setText(matchInfo.getStateDescription());

			childViewHolder.rlMatchContainer.setVisibility(View.VISIBLE);
			childViewHolder.rlEmptyContainer.setVisibility(View.GONE);
			childViewHolder.rlProgress.setVisibility(View.GONE);

		} else if (state == GroupState.empty) {
			childViewHolder.rlMatchContainer.setVisibility(View.GONE);
			childViewHolder.rlEmptyContainer.setVisibility(View.VISIBLE);
			childViewHolder.rlProgress.setVisibility(View.GONE);
		} else {
			childViewHolder.rlMatchContainer.setVisibility(View.GONE);
			childViewHolder.rlEmptyContainer.setVisibility(View.GONE);
			childViewHolder.rlProgress.setVisibility(View.VISIBLE);
		}
		return convertView;
	}

	@Override
	public int getChildSize(int groupPosition) {
		return mChildList.get(groupPosition).size();
	}

	@Override
	public MatchInfo getChild(int groupPosition, int childPosition) {
		return mChildList.get(groupPosition).get(childPosition);
	}

	class ChildViewHolder {
		RelativeLayout rlMatchContainer;

		TextView tvTimeState;
		TextView tvLeagueName;
		TextView tvHostNumber;
		TextView tvHostName;
		TextView tvHostScore;
		TextView tvGuestNumber;
		TextView tvGuestName;
		TextView tvGuestScore;

		RelativeLayout rlPeilvContainer;
		TextView tvResult;
		TextView tvResultValue;

		RelativeLayout rlEmptyContainer;

		RelativeLayout rlProgress;
	}

}
